package com.sxj.shop.controller;

import com.sxj.shop.entity.Product;
import com.sxj.shop.entity.User;
import com.sxj.shop.exception.ProductNotExistException;
import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserExistException;
import com.sxj.shop.exception.UserNotExistException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.ProductService;
import com.sxj.shop.service.UserService;
import com.sxj.shop.util.PageInfo;

import java.util.List;
import java.util.Scanner;

public class UserController {

    private Scanner sc = new Scanner(System.in);
    // 代理对象
    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");

    public static void main(String[] args) {
        new UserController().showMenu();
    }

    public void showMenu() {
        System.out.println("***************************欢迎来到紫金商城***************************");
        System.out.print("请选择服务(1.注册 2.登录 3.查询商品信息 4.分页查询):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findAll();
                break;
            case 4:
                findPage();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入!");
                showMenu();
                break;
        }
    }

    public void register() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        // 对于密码,可以输入两次,两次必须一致
        // 输错三次后结束
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入电话:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();
        try {
            // 调用业务层方法实现注册功能
            userService.register(username, password, phone, address);
            System.out.println("提示:恭喜您,注册成功!");
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:注册失败,失败原因:" + e.getMessage());
        } catch (UserExistException e) {
            System.out.println("错误:注册失败,失败原因:" + e.getMessage());
        }
        showMenu();
    }

    public void login() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();

        try {
            // 调用业务层方法
            User user = userService.login(username, password);

            // 登录成功之后进入主菜单
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("登录失败,失败原因:" + e.getMessage());
            showMenu();
        } catch (UserNotExistException e) {
            System.out.println("登录失败,失败原因:" + e.getMessage());
            showMenu();
        }
    }

    public void findAll() {
        // 商品编号     商品名称        商品单价
        // 1        威图高端商务手机    100000
        // 2        克里特(kreta）KDEV200    39800
        // ...
        List<Product> products = productService.findAll();
        if (!products.isEmpty()) {
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
        }
        showMenu();
    }

    public void findPage() {
        // 输入第几页
        System.out.print("请输入查询第几页数据:");
        String pageNo = sc.next();
        // 输入每页显示几条数据
        System.out.print("请输入每页显示几条数据:");
        String pageSize = sc.next();

        try {
//            List<Product> products = productService.findPage(pageNo, pageSize);
            PageInfo<Product> productPage = productService.findPage(pageNo, pageSize);
            List<Product> products = productPage.getList();
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
            System.out.println("当前页:" + productPage.getPageNo() + ",上一页:" + productPage.getPrePage() + ",下一页:" + productPage.getNextPage() + ",总页数:" + productPage.getPages());
        } catch (ProductNotExistException e) {
            System.out.println("查询失败,失败原因:" + e.getMessage());
        }
        showMenu();
    }
}
